<template>
  <div>
    <!-- <h2>新增地址</h2> -->
    <van-address-edit
      :area-list="areaList"
      show-postal
      show-delete
      show-set-default
      show-search-result
      :search-result="searchResult"
      :address-info="addressinfo"
      :area-columns-placeholder="['请选择', '请选择', '请选择']"
      @save="onSave"
      @delete="onDelete"
    />
  </div>
</template>

<script>
import Vue from "vue";
import { AddressEdit } from "vant";
import { areaList } from "@vant/area-data";
Vue.use(AddressEdit);
import { Toast } from "vant";

import { addressadd, addresslist,addressedit,addressremove} from "../../utils/api";
import { mapGetters } from "vuex";

export default {
  data() {
    return {
      areaList,
      searchResult: [],
      addressinfo: {},
      bianjilist:{}
    };
  },
  computed: {
    ...mapGetters({
      userlist: "user",
    }),
  },
  mounted() {
    // console.log(this.userlist);
  },
  methods: {
    // 点击提交
    onSave(context) {
      // 如果默认信息不存在则是添加
      if (!this.addressinfo.name) {
        let list = {
          uid: this.userlist.uid,
          username: context.name,
          userphone: context.tel,
          location: context.city,
          useraddress: context.addressDetail,
        };
        addressadd(list).then((res) => {
          console.log(res);
          if (res.data.code == 200) {
            Toast("添加成功");
            this.$emit("add");
          }
        });
      } else {
        // 默认信息存在，则编辑
        // console.log(2222);
        let list = {
          id: context.id,
          username: context.name,
          userphone: context.tel,
          location: context.city,
          useraddress: context.addressDetail,
        };
        addressedit(list).then(res=>{          
          if (res.data.code == 200) {
            Toast("编辑成功");
            this.$emit("add");
          }
        })
      }

    },
    // 获取一条信息
    getinfo(id, uid) {
      // console.log(id,uid);
      addresslist({ uid: uid }).then((res) => {
        console.log(res.data.list[id]);
        this.bianjilist=res.data.list[id]
        this.addressinfo = {
          ...res.data.list[id],
          name: res.data.list[id].username,
          tel: res.data.list[id].userphone,
          addressDetail: res.data.list[id].useraddress,
          city: "吉安市",
          province: "江西省",
        };
      });
    },
    // 确认删除
    onDelete() {
      Toast("删除成功");
      addressremove({id:this.bianjilist.id}).then(res=>{
        if (res.data.code == 200) {
            Toast("删除成功");
            this.$emit("add");
          }
      })
    },
  },
};
</script>

<style scoped>
</style>